/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_DB_SQLOPERATION_H_
#define SHARED_DB_SQLOPERATION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include "Shared/Base/Mutex.h"
#include "Shared/Util/LockedQueue.h"
#include "Shared/DB/Callback.h"


namespace Shared {
namespace DB {
    class SqlConnection;
    class SqlStmtParameters;


    ///////////////////////////////////////////////////////////////////////////
    class SqlOperation
    {
    public:
        virtual ~SqlOperation(){}
        virtual bool execute(SqlConnection *) = 0;
    };


    ///////////////////////////////////////////////////////////////////////////
    class SqlPreparedRequest : public SqlOperation
    {
    public:
        SqlPreparedRequest(int index, SqlStmtParameters* arg);
        virtual ~SqlPreparedRequest();

        bool execute(SqlConnection* conn) override;

    private:
        const int _index;
        SqlStmtParameters* _param;
    };


    ///////////////////////////////////////////////////////////////////////////
    class SqlResultQueue : public Util::LockedQueue<IQueryCallback* , Mutex>
    {
    public:
        SqlResultQueue() {}
        void update();
    };


    ///////////////////////////////////////////////////////////////////////////
    class SqlQuery : public SqlOperation
    {
    public:
        SqlQuery(const char* sql, IQueryCallback* callback, SqlResultQueue* queue)
            : _sql(sql), _callback(callback), _queue(queue) {}
        ~SqlQuery() {  }
        bool execute(SqlConnection* conn) override;

    private:
        const std::string _sql;
        IQueryCallback* _callback;
        SqlResultQueue* _queue;
    };

}}

#endif // SHARED_DB_SQLOPERATION_H_
